//
//  addSchoolViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/15.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "addSchoolViewController.h"

@interface addSchoolViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong) UITextField *nameTextField;
@property(nonatomic,strong) UITextView *messageTextView;

@property(nonatomic,strong) UIScrollView *mianScrollView;

@end

@implementation addSchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self photo];
}

#pragma mark ------------------ init -------------------

- (void)photo{
    
    _mianScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenScale(216) + 64, scrWidth(), (( scrWidth()-screenScale(45) +8)/3)*2 )];
    _mianScrollView.contentSize = CGSizeMake(scrWidth(), (( scrWidth()-screenScale(45) )/3)*2);
    _mianScrollView.bounces = YES;
    _mianScrollView.showsVerticalScrollIndicator = false;
    _mianScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mianScrollView];
    [_mianScrollView setDelegate:self];
    self.showInView = _mianScrollView;
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrWidth()/3*2, screenScale(44 * 4), scrWidth()/3, (scrWidth()-screenScale(45))/3+8 )];
//    view.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:view];
    
    self.maxCount = 6;
    [self initPickerView];
}

- (void)initView{
    
    self.title = @"激活学校";
    self.view.backgroundColor = getColor(@"ffffff");
    
    UILabel *topLabel = [ConfigXp labelWithFrame:CGRectMake(0, 0, scrWidth() - screenScale(15), screenScale(44))
                                            text:@"    添加学校信息"
                                       textColor:getColor(@"333333")
                                            font:get4sFont(13)
                                   textAlignment:NSTextAlignmentLeft
                                 backgroundColor:getColor(@"f5f5f5")];
    [self.view addSubview:topLabel];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(44), scrWidth(), screenScale(348))];
    bgView.backgroundColor = getColor(@"ffffff");
    [self.view addSubview:bgView];
    
    ///
    
    UILabel *oldPwdLabel = [ConfigXp labelWithFrame:CGRectZero
                                               text:@"学校名称"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [self.view addSubview:oldPwdLabel];
    [oldPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(15+44));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _nameTextField = [[UITextField alloc]init];
    _nameTextField.placeholder = @"填写你的学校名称";
    _nameTextField.textColor = getColor(@"4a4a4a");
    _nameTextField.font = get4sFont(12);
    _nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_nameTextField];
    [_nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(17+44));
        make.left.mas_equalTo(self.view).with.offset(screenScale(87));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *oldThreadView = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(44+44), scrWidth() - screenScale(15), screenScale(0.5))];
    oldThreadView.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:oldThreadView];
    
    ///
    
    UILabel *introductionLabel = [ConfigXp labelWithFrame:CGRectZero
                                               text:@"学校简介"
                                          textColor:getColor(@"4a4a4a")
                                               font:get4sFont(13)
                                      textAlignment:NSTextAlignmentLeft
                                    backgroundColor:[UIColor clearColor]];
    [self.view addSubview:introductionLabel];
    [introductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(oldThreadView.mas_bottom).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _messageTextView = [[UITextView alloc]init];
    _messageTextView.font = get4sFont(12);
    _messageTextView.backgroundColor = getColor(@"ffffff");
    _messageTextView.textColor = getColor(@"000000");
    [self.view addSubview:_messageTextView];
    [_messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(44*3));
        make.left.mas_equalTo(self.view).with.offset(screenScale(10));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-10));
        make.height.mas_equalTo(screenScale(100));
    }];
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"介绍一下你的学校吧～";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.font = get4sFont(12);
    
    placeHolderLabel.textColor = getColor(@"9b9b9b");
    [placeHolderLabel sizeToFit];
    [_messageTextView addSubview:placeHolderLabel];
    [_messageTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    
    ///
    
    UIView *messageThreadView = [[UIView alloc]init];
    messageThreadView.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:messageThreadView];
    [messageThreadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_messageTextView.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        make.right.mas_equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(screenScale(0.5));
    }];
    
    UILabel *imgLabel = [ConfigXp labelWithFrame:CGRectZero
                                                     text:@"学校图片"
                                                textColor:getColor(@"4a4a4a")
                                                     font:get4sFont(13)
                                            textAlignment:NSTextAlignmentLeft
                                          backgroundColor:[UIColor clearColor]];
    [self.view addSubview:imgLabel];
    [imgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(messageThreadView.mas_bottom).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
//    UIButton *imgBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(15), screenScale(216), screenScale(110), screenScale(110))
//                                           title:nil
//                                      titleColor:nil
//                                            font:nil
//                                 backgroundColor:nil
//                                           image:[UIImage imageNamed:@"add_image_icon"]];
//    [imgBtn addTarget:self action:@selector(imgBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:imgBtn];
//    [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(messageThreadView.mas_bottom).with.offset(screenScale(44));
//        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
//        make.width.mas_equalTo(screenScale(110));
//        make.height.mas_equalTo(screenScale(110));
//    }];
    
    ///
    
//    UIButton *finishBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(30), screenScale(414), screenScale(315), screenScale(44))
//                                              title:@"激活学校"
//                                         titleColor:getColor(@"ffffff")
//                                               font:get4sFont(13)
//                                    backgroundColor:getColor(@"3a95ff")
//                                              image:nil];
//    finishBtn.layer.cornerRadius = screenScale(3);
//    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:finishBtn];
    
    UIButton *finishBtn = [ConfigXp buttonWithFrame:CGRectZero
                                              title:@"激活学校"
                                         titleColor:getColor(@"ffffff")
                                               font:get4sFont(13)
                                    backgroundColor:getColor(@"3a95ff")
                                              image:nil];
    finishBtn.layer.cornerRadius = screenScale(3);
    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finishBtn];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(0);
        make.right.mas_equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(screenScale(49));
    }];
}

#pragma mark ------------------ click -------------------

- (void)imgBtnClick{

    
}

- (void)finishBtnClick{
    
    if (_nameTextField.text.length < 1) {
        
        [PublicFunction showAlert:nil message:@"请填写学校名称" duration:1.0f];
        return;
        
    }
    
    if (self.getBigImageArray.count < 1) {
        
        NSLog(@"bigImageArray===%@",self.getBigImageArray);
        [PublicFunction showAlert:nil message:@"请选择学校照片" duration:1.0];
        return;
        
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _area_id forKey:@"area_id"];//市id
    [productParams setValue: _parent_id forKey:@"parent_id"];//省id
    [productParams setValue: _nameTextField.text forKey:@"name"];
    [productParams setValue: _messageTextView.text forKey:@"content"];
    
    NSLog(@"productParams===\n\n%@",productParams);
    
    //两种方式上传头像
    /*方式一：使用NSData数据流传图片*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/plain",
                                                         @"text/javascript",
                                                         @"text/json",
                                                         @"text/html",
                                                         @"image/jpeg", nil];
    [manager POST:more_editData parameters:productParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        __block NSInteger i = 0;
        [self.bigImageArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //压缩图片
            i++;
            // NSData *imageData = UIImagePNGRepresentation(obj);
            NSData *imageData = UIImageJPEGRepresentation(obj,0.005);
            
            
            //压缩图片
            
            //拼接图片名称+时间确保不重复命名
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *imageFileName = [NSString stringWithFormat:@"%@%ld.jpg", [formatter stringFromDate:[NSDate date]],i];
            // 测试输出文件名
            
            //上传图片，以文件流的格式
            
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%ld",i] fileName:imageFileName mimeType:@"image/JPG"];
            
            
        }];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *requestTmp = [NSString stringWithString: strtmp];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];  //解析
        
        //        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        
        NSLog(@"+++>%@", resultDic);
        
        NSLog(@"--->%@", resultDic[@"msg"]);
        
        [hud removeFromSuperview];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--->%@", error);
        
        [hud removeFromSuperview];
        
    }];
}

@end
